package com.ruoyi.electricity.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.electricity.domain.ElectricityMeterData;
import com.ruoyi.electricity.domain.vo.GisEquipVo;
import com.ruoyi.rsikManage.domain.vo.GisMonitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.electricity.domain.ElectricityData;
import com.ruoyi.electricity.service.ElectricityDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用电Controller
 *
 * @author ljw
 * @date 2023-08-29
 */

@RestController
@RequestMapping("/electricity/electricityData")
public class ElectricityDataController extends BaseController
{
    @Autowired
    private UndergroundElectricitySystem undergroundElectricitySystem;
    @Autowired
    private ElectricityDataService electricityDataService;

    /**
     * 查询所有gis用电设备
     */
    @GetMapping("allGisEquips")
    public TableDataInfo selectAllGisEquips(GisEquipVo gisEquipVo) {
        startPage();
        List<GisEquipVo> res = electricityDataService.selectAllGisEquips(gisEquipVo);
        return getDataTable(res);
    }
    /**
     * 查询有坐标的gis用电设备
     */
    @GetMapping("gisEquipsHasCoordinates")
    public TableDataInfo gisEquipsHasCoordinates() {
        startPage();
        List<GisEquipVo> res = electricityDataService.selectGisEquipsHasCoordinates();
        return getDataTable(res);
    }
    /**
     * 配置用电设备的Gis坐标
     */
    @PutMapping("updateGisEquipsCoordinates")
    private AjaxResult updateGisEquipsCoordinates(@Validated @RequestBody GisEquipVo gisEquipVo) {
        electricityDataService.updateGisEquipsCoordinates(gisEquipVo);
        return AjaxResult.success();
    }
    /**
     * 查询用户设备的历史数据
     */
    @GetMapping("history")
    private TableDataInfo historyList(ElectricityData electricityData, Date startTime, Date endTime) {
        startPage();
        List<ElectricityData> list = electricityDataService.selectElectricityHistoryDataList(electricityData,  startTime,  endTime);
        return getDataTable(list);
    }
    /**
     * 查询井下实时用电数据
     * @return
     */
    @GetMapping("realTimeDataV2")
    private String getRealTimeData() {
        return undergroundElectricitySystem.getRealTimeElectricityData();
    }

    /**
     * 查询井下实时用电数据
     * @return
     */
    @GetMapping("realTimeData")
    private TableDataInfo  fetchMeterEnergyData()  throws IOException {
        List<ElectricityMeterData> electricityMeterData = undergroundElectricitySystem.fetchMeterEnergyData();
        return getDataTable(electricityMeterData);
    }

    /**
     * 查询用电列表 （之后需要加上设备类型字段、 报警阈值、 设备重要等级）
     */
    @GetMapping("/list")
    public TableDataInfo list(ElectricityData electricityData)
    {
        startPage();
        List<ElectricityData> list = electricityDataService.selectElectricityDataList(electricityData);
        return getDataTable(list);
    }

    /**
     * 导出用电列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectricityData electricityData)
    {
        List<ElectricityData> list = electricityDataService.selectElectricityDataList(electricityData);
        ExcelUtil<ElectricityData> util = new ExcelUtil<ElectricityData>(ElectricityData.class);
        util.exportExcel(response, list, "用电数据");
    }

//    /**
//     * 查询用电列表 （之后需要加上设备类型字段、 报警阈值、 设备重要等级）
//     */
//    @GetMapping("/today/list")
//    public TableDataInfo selectElectricityDataTodayList(ElectricityData electricityData)
//    {
//        startPage();
//        List<ElectricityData> list = electricityDataService.selectElectricityDataTodayList(electricityData);
//        return getDataTable(list);
//    }


    /**
     * 查询用电列表 （之后需要加上设备类型字段、 报警阈值、 设备重要等级）
     */
    @GetMapping("/output/meterenergy")
    public String output(ElectricityData electricityData)
    {

        String a = "[\n" +
                "    {\n" +
                "        \"MeterID\": 1,\n" +
                "        \"MeterName\": \"灵石一线\",\n" +
                "        \"Voltage\": 60.7,\n" +
                "        \"Electric\": 0,\n" +
                "        \"FAQValue\": 0,\n" +
                "        \"PAEnergy\": 202804,\n" +
                "        \"ReEnergy\": 4616,\n" +
                "        \"PowerFactor\": 0,\n" +
                "        \"InsertTime\": \"2024-06-12 14:06:16\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"MeterID\": 2,\n" +
                "        \"MeterName\": \"马和二回\",\n" +
                "        \"Voltage\": 61.3,\n" +
                "        \"Electric\": 108,\n" +
                "        \"FAQValue\": -2.72,\n" +
                "        \"PAEnergy\": 90896,\n" +
                "        \"ReEnergy\": 9868,\n" +
                "        \"PowerFactor\": 0,\n" +
                "        \"InsertTime\": \"2024-06-12 14:06:16\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"MeterID\": 3,\n" +
                "        \"MeterName\": \"井下一回\",\n" +
                "        \"Voltage\": 60.8,\n" +
                "        \"Electric\": 0,\n" +
                "        \"FAQValue\": 0,\n" +
                "        \"PAEnergy\": 48828,\n" +
                "        \"ReEnergy\": 0,\n" +
                "        \"PowerFactor\": 0,\n" +
                "        \"InsertTime\": \"2024-06-12 14:06:16\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"MeterID\": 4,\n" +
                "        \"MeterName\": \"井下二回\",\n" +
                "        \"Voltage\": 61.2,\n" +
                "        \"Electric\": 80,\n" +
                "        \"FAQValue\": 5.62,\n" +
                "        \"PAEnergy\": 521456,\n" +
                "        \"ReEnergy\": 13238,\n" +
                "        \"PowerFactor\": 0.999,\n" +
                "        \"InsertTime\": \"2024-06-12 14:06:17\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"MeterID\": 6,\n" +
                "        \"MeterName\": \"副斜井\",\n" +
                "        \"Voltage\": 60.8,\n" +
                "        \"Electric\": 24,\n" +
                "        \"FAQValue\": 1.41,\n" +
                "        \"PAEnergy\": 208707,\n" +
                "        \"ReEnergy\": 5009,\n" +
                "        \"PowerFactor\": 0.965,\n" +
                "        \"InsertTime\": \"2024-06-12 14:06:17\"\n" +
                "    }\n" +
                "]";
        return a;
    }
}
